﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Security.AccessControl;
using System.Threading;

namespace CppUT
{
    namespace Common
    {
        public class Log
        {
            public static void Initialize(string filename)
            {
                _sLogFile = filename;
                _slogMutex = new Mutex();
                File.WriteAllText(_sLogFile, "");
            }
            
            public static void MethodEntry()
            {
                lock (_slogMutex)
                {
                    var stackTrace = new StackTrace();
                    var stackFrame = stackTrace.GetFrame(1);

                    var logString = new StringBuilder("Entered " + stackFrame.GetMethod().Name + Environment.NewLine);
                    Debug.WriteLine(logString);

                    if (_sLogFile != null) File.AppendAllText(_sLogFile, logString.ToString());    
                }
            }

            public static void MethodExit()
            {
                lock (_slogMutex)
                {
                    var stackTrace = new StackTrace();
                    var stackFrame = stackTrace.GetFrame(1);

                    var logString = new StringBuilder("Exited " + stackFrame.GetMethod().Name + Environment.NewLine);
                    Debug.WriteLine(logString);
                    if (_sLogFile != null) File.AppendAllText(_sLogFile, logString.ToString());
                }
            }

            public static void Info(string info)
            {
                lock (_slogMutex)
                {
                    Debug.WriteLine(info);
                    if (_sLogFile != null) File.AppendAllText(_sLogFile, info + Environment.NewLine);
                }
            }

            private static string _sLogFile;
            private static Mutex _slogMutex;
        }
    }
}